//
//  XGTopicModel.m
//  BSBuDeJie
//
//  Created by monkey on 2018/12/21.
//  Copyright © 2018 itcast. All rights reserved.
//

#import <MJExtension/MJExtension.h>

#import "XGTopicModel.h"
#import "XGTopCommentModel.h"

#import "XGWordTopicTableViewCell.h"

@implementation XGTopicModel

@synthesize rowHeight = _rowHeight;

+ (void)load
{
    [self mj_setupReplacedKeyFromPropertyName121:^id(NSString *propertyName) {
        if ([propertyName isEqualToString:@"timeId"]) {
            return @"t";
        } else if ([propertyName isEqualToString:@"topCmt"]) {
            return @"top_cmt[0]";
        } else if ([propertyName isEqualToString:@"imageURL"]) {
            return @"image0";
        }
        else {
            return [propertyName mj_underlineFromCamel];
        }
    }];
}

- (CGFloat)rowHeight
{
    if (_rowHeight <= 0) {
        // topView
        _rowHeight += kTopCellTopViewHeight + kMargin;
        
        // 文本内容
        _rowHeight += [self.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 2 * kMargin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height + kMargin;
        
        // 背景大图
        if (self.imageURL.length > 0) {
            CGFloat imageHeight = (SCREEN_WIDTH - 2 * kMargin) / self.width * self.height;
            // 如果背景图片超过屏幕宽度 设置为200 这里修改为其他高度 每个cell对应的setter模型方法也要改
            imageHeight = imageHeight > (SCREEN_HEIGHT * 0.8) ? kDefaultPictureHeight : imageHeight;
            _rowHeight += imageHeight + kMargin;
        }
        
        // 热评
        if (self.topCmt != nil) {
            _rowHeight += 30 + kMargin;
            NSString *topComment = self.topCmt.content.length > 0 ? self.topCmt.content : @"[语音]";
            _rowHeight += [topComment boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 2 * kMargin, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15]} context:nil].size.height + kMargin;
        }
       
        // bottomView
        _rowHeight += kTopCellBottomViewHeight;
    }
    
    return _rowHeight;
}

- (BOOL)isBigImage
{
    return self.height > SCREEN_HEIGHT;
}

@end
